/**
 * 截取区域数据
 */

#include "eccodes.h"

int sfx_box()
{
    int err = 0;

    char* filename = "d:/sfxData/ECMWF/eccodes_test_data/reduced_gaussian_model_level.grib1";

    FILE* f = fopen(filename, "rb");

    codes_context* c = codes_context_get_default();
    codes_handle* h  = codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
    codes_box* box   = codes_box_new(h, &err);

    double north = 60;
    double south = 10;
    double west  = -10;
    double east  = 30;


    //
    codes_points* points = codes_box_get_points(box, north, west, south, east, &err);
    double* val          = (double*)malloc(sizeof(double) * points->n);
    codes_points_get_values(h, points, val);

    for (int i = 0; i < points->n; i++) {
        printf("%d -- %.3f %.3f %ld %g\n", i,
               points->latitudes[i], points->longitudes[i], (long)(points->indexes[i]), val[i]);
    }

    // free memory
    free(val);
    codes_handle_delete(h);
    fclose(f);
    return 0;
}
